﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using BLL;
using System.IO;
using AjaxControlToolkit;
using System.Web.Script.Serialization;
using BLL.Pictures;

public partial class AsyncFileUploadSimple : System.Web.UI.UserControl
{
    public int PictureId { get; set; }

    //public string PictureUrl { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        //string pictureUrl = Picture.GetPictureUrl(PictureId, 100, false, null);
        //ScriptManager.RegisterClientScriptBlock(AsyncFileUpload1, AsyncFileUpload1.GetType(), "img", "top.document.getElementById('imgUpload').src='" + pictureUrl + "'", true);
    }

    protected void AsyncFileUploadComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string contentType = "";
        string fileName = AsyncFileUpload1.PostedFile.FileName;
        string seofileName = Helpers.GetSeName(Path.GetFileNameWithoutExtension(fileName), false, false);
        Stream stream = AsyncFileUpload1.PostedFile.InputStream;
        byte[] pictureBinary = new byte[stream.Length];
        stream.Read(pictureBinary, 0, pictureBinary.Length);

        string fileExtension = Path.GetExtension(fileName);
        if (!String.IsNullOrEmpty(fileExtension))
            fileExtension = fileExtension.ToLowerInvariant();

        if (String.IsNullOrEmpty(contentType))
        {
            switch (fileExtension)
            {
                case ".bmp":
                    contentType = "image/bmp";
                    break;
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".tiff":
                case ".tif":
                    contentType = "image/tiff";
                    break;
                default:
                    break;
            }
        }

        int ret = Picture.InsertPicture(pictureBinary, contentType, seofileName, true);
        Session["PictureId"] = ret;
        string pictureUrl = Picture.GetPictureUrl(ret, 100, false, null);
        ScriptManager.RegisterClientScriptBlock(AsyncFileUpload1, AsyncFileUpload1.GetType(), "img", "top.document.getElementById('imgUpload').src='" + pictureUrl + "'", true);
    }
}
